package com.tanxin.bill.common.data;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;

import io.objectbox.annotation.Convert;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
import io.objectbox.converter.PropertyConverter;

@Entity
public class CustomerData implements Parcelable {

    @Id
    private long id;
    private String name;
    private String phone;
    private Date handleDate;

    @Convert(converter = CustomerData.TypeConverter.class, dbType = Integer.class)
    private CustomerData.Type type;

    public enum Type{
        /**
         * 默认状态
         * */
        DEFAULT(0),
        /**
         * 未到期
         * */
        NO_EXPIRE(1),
        /**
         * 结清
         * */
        COMPLETE(2),
        /**
         * 逾期
         * */
        OVERDUE(3);
        final int flag;
        Type(int flag){
            this.flag = flag;
        }
    }



    public CustomerData() {
    }


    public static class TypeConverter implements PropertyConverter<CustomerData.Type, Integer> {
        @Override
        public CustomerData.Type convertToEntityProperty(Integer databaseValue) {
            if (databaseValue == null) {
                return null;
            }
            for (CustomerData.Type type : CustomerData.Type.values()) {
                if (type.flag == databaseValue) {
                    return type;
                }
            }
            return Type.DEFAULT;
        }

        @Override
        public Integer convertToDatabaseValue(CustomerData.Type entityProperty) {
            return entityProperty == null ? null : entityProperty.flag;
        }
    }

    public CustomerData.Type getType() {
        return type;
    }

    public void setType(CustomerData.Type type) {
        this.type = type;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    public Date getHandleDate() {
        return handleDate;
    }

    public void setHandleDate(Date handleDate) {
        this.handleDate = handleDate;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeString(this.name);
        dest.writeString(this.phone);
        dest.writeLong(this.handleDate != null ? this.handleDate.getTime() : -1);
        dest.writeInt(this.type == null ? -1 : this.type.ordinal());
    }

    protected CustomerData(Parcel in) {
        this.id = in.readLong();
        this.name = in.readString();
        this.phone = in.readString();
        long tmpHandleDate = in.readLong();
        this.handleDate = tmpHandleDate == -1 ? null : new Date(tmpHandleDate);
        int tmpType = in.readInt();
        this.type = tmpType == -1 ? null : Type.values()[tmpType];
    }

    public static final Parcelable.Creator<CustomerData> CREATOR = new Parcelable.Creator<CustomerData>() {
        @Override
        public CustomerData createFromParcel(Parcel source) {
            return new CustomerData(source);
        }

        @Override
        public CustomerData[] newArray(int size) {
            return new CustomerData[size];
        }
    };
}
